/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package readwriteofficedocs;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.hpsf.CustomProperties;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/**
 *
 * @author Administrador
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
//        String filesname = "C:\\ejemplo.doc";
//        POIFSFileSystem fs = null;
        try {
//            fs = new POIFSFileSystem(new FileInputStream(filesname));
//            //Couldn't close the braces at the end as my site did not allow it to close
//
//            HWPFDocument doc = new HWPFDocument(fs);
//
//
//            WordExtractor we = new WordExtractor(doc);
//
//
//            String[] paragraphs = we.getParagraphText();
//
//            for (String parrafo : paragraphs) {
//                System.out.print(parrafo.replace("NOMBRE_USUARIO", "WATECITO"));
//            }
            // POI apparently can't create a document from scratch,
            // so we need an existing empty dummy document

            File f = new File("C:\\plantilla.doc");
            POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(f));
            HWPFDocument doc = new HWPFDocument(fs);
            WordExtractor we = new WordExtractor(doc);
//            String[] parrafos = we.getParagraphText();
//            for (String parrafo : parrafos) {
//                System.out.print(parrafo.replace("NOMBRE_USUARIO", "WATECITO"));
//            }

            for (int i = 0; i < doc.getRange().numParagraphs(); i++) {
                Paragraph par = doc.getRange().getParagraph(i);
                doc.getRange().replaceText("NOMBRE_USUARIO", "");

//                par.replaceText("NOMBRE_USUARIO", "WALTER");
//                par.replaceText("NRO_DNI", "43769130");
                
                //String textoParrafo = par.text();
//                for (int j = 0; j < par.numCharacterRuns(); j++) {
//                    CharacterRun cr = par.getCharacterRun(j);
//                    String textoCR = cr.text();
//                    if (textoCR.contains("NOMBRE_USUARIO")) {
//                        int start = textoCR.indexOf("NOMBRE_USUARIO");
//                        cr.replaceText("NOMBRE_USUARIO", "WATECITO", start);
//
//                    }
//                }
//                par.
            }





//            doc.getRange().replaceText("NOMBRE_USUARIO", "EMMANUEL WALTER");
//            doc.getRange().replaceText("NRO_DNI", "4376913");

            //System.out.print(we.getTextFromPieces());



            /*
            // centered paragraph with large font size
            Range range = doc.getRange();
            Paragraph par1 = range.insertAfter(new ParagraphProperties(), 0);
            par1.setSpacingAfter(200);
            par1.setJustification((byte) 1);
            // justification: 0=left, 1=center, 2=right, 3=left and right

            CharacterRun run1 = par1.insertAfter("one");
            run1.setFontSize(2 * 18);
            // font size: twice the point size

            // paragraph with bold typeface
            Paragraph par2 = run1.insertAfter(new ParagraphProperties(), 0);
            par2.setSpacingAfter(200);
            CharacterRun run2 = par2.insertAfter("two two two two two two two two two two two two two");
            run2.setBold(true);
            
            // paragraph with italic typeface and a line indent in the first line
            Paragraph par3 = run2.insertAfter(new ParagraphProperties(), 0);
            par3.setFirstLineIndent(200);
            par3.setSpacingAfter(200);
            CharacterRun run3 = par3.insertAfter("three three three three three three three three three " + "three three three three three three three three three three three three three three " + "three three three three three three three three three three three three three three");
            run3.setItalic(true);
             */
            // add a custom document property (needs POI 3.5; POI 3.2 doesn't save custom properties)
            DocumentSummaryInformation dsi = doc.getDocumentSummaryInformation();
            CustomProperties cp = dsi.getCustomProperties();
            if (cp == null) {
                cp = new CustomProperties();
            }
            cp.put("myProperty", "foo bar baz");
            dsi.setCustomProperties(cp);
            FileOutputStream fos = new FileOutputStream("C:\\nuevoCONDATA.doc");
            doc.write(fos);
            fos.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
